/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.com.apresentacao.pedagogico;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import src.com.negocio.pedagogico.CadastrarIdioma;
import src.com.negocio.pedagogico.CadastrarNivelIdioma;
import src.com.dados.pedagogico.NivellDao;

/**
 *
 * @author Giane
 */
public class CadastroNivelCurso extends javax.swing.JFrame {

    private String nm_nivel;

    /**
     * Creates new form CadastroNivelCurso
     */
    public CadastroNivelCurso() throws SQLException {
        initComponents();
        CadastrarIdioma c = new CadastrarIdioma();
        for (int i = 0; i < c.consultaIdioma().size(); i++) {
            i_idioma.addItem(c.consultaIdioma().get(i));
            //System.out.print(c.consultaIdioma().get(i));            
        }


        System.out.println("n entrou");
        //i_idioma.addItem("item5");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        i_idioma = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        nmNivel = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        meses = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        codNivel = new javax.swing.JTextField();
        cadastrar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        numTurmas = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        v_valor = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jInternalFrame1.setTitle("Cadastro - Nivel de Idioma");
        jInternalFrame1.setVisible(true);

        jLabel1.setText("Idioma: ");

        i_idioma.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                i_idiomaItemStateChanged(evt);
            }
        });
        i_idioma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                i_idiomaActionPerformed(evt);
            }
        });

        jLabel2.setText("Nível: ");

        jLabel3.setText("Duração: ");

        meses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mesesActionPerformed(evt);
            }
        });

        jLabel4.setText("meses");

        jLabel5.setText("Código: ");

        cadastrar.setText("Cadastrar");
        cadastrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cadastrarMouseClicked(evt);
            }
        });

        jButton1.setText("Remover");
        jButton1.setEnabled(false);
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel6.setText("Nº Máximo de Turmas: ");

        jLabel7.setText("Valor:");

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cadastrar))
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(codNivel))
                            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(i_idioma, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(nmNivel))
                            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numTurmas))
                            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(meses, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4)))
                        .addGap(0, 113, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addGap(18, 18, 18)
                .addComponent(v_valor, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(i_idioma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(codNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nmNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(numTurmas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(meses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(v_valor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cadastrar)
                    .addComponent(jButton1))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mesesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mesesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mesesActionPerformed

    private void cadastrarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cadastrarMouseClicked

        String idioma = i_idioma.getSelectedItem().toString();
        String codigo = codNivel.getText().toString();
        String nivel = nmNivel.getText().toString();
        String duracao = meses.getText().toString();
        String qtdTurmas = numTurmas.getText().toString();
        String valor = v_valor.getText().toString();

        CadastrarNivelIdioma E = new CadastrarNivelIdioma();
        try {
            if (E.VerificarNivel(codigo).equals("0")) {
                E.insereNivel(idioma, codigo, nivel, duracao, qtdTurmas,valor);
                javax.swing.JDialog inf = new javax.swing.JDialog();
                inf.setSize(250, 150);
                javax.swing.JOptionPane.showMessageDialog(inf, "Cadastro realizado com Sucesso!", "Informação", javax.swing.JOptionPane.INFORMATION_MESSAGE);
System.out.println("bakbakbabka");
            } else {
                javax.swing.JDialog inf = new javax.swing.JDialog();
                inf.setSize(250, 150);
                javax.swing.JOptionPane.showMessageDialog(inf, "O codigo informado já existe!", "Informação", javax.swing.JOptionPane.WARNING_MESSAGE);

            }
        } catch (SQLException ex) {
            Logger.getLogger(CadastroNivelCurso.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_cadastrarMouseClicked

    private void i_idiomaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_i_idiomaItemStateChanged
//          i_idioma.addItem("item5");
    }//GEN-LAST:event_i_idiomaItemStateChanged

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        CadastrarNivelIdioma ver = new CadastrarNivelIdioma();
        try {
            String count = ver.verificaMatricula(nmNivel.getText().toString());

            int resposta = javax.swing.JOptionPane.showConfirmDialog(null, "Tem certeza que desaja excluir Turma?");

            if (resposta == JOptionPane.YES_OPTION) {
                if (count.equals("0")) {
                    ver.removeNivel(nmNivel.getText().toString());
                    javax.swing.JDialog inf = new javax.swing.JDialog();
                    inf.setSize(250, 150);
                    javax.swing.JOptionPane.showMessageDialog(inf, "Nivel excluido com sucesso!", "Informação", javax.swing.JOptionPane.INFORMATION_MESSAGE);
                } else {
                    javax.swing.JDialog inf = new javax.swing.JDialog();
                    inf.setSize(250, 150);
                    javax.swing.JOptionPane.showMessageDialog(inf, "Nivel não pode ser excluido. Existe Aluno Matriculado!", "Informação", javax.swing.JOptionPane.WARNING_MESSAGE);
               }
            }




        } catch (SQLException ex) {
            Logger.getLogger(CadastroNivelCurso.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void i_idiomaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_i_idiomaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_i_idiomaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastroNivelCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastroNivelCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastroNivelCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastroNivelCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new CadastroNivelCurso().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(CadastroNivelCurso.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cadastrar;
    private javax.swing.JTextField codNivel;
    private javax.swing.JComboBox i_idioma;
    private javax.swing.JButton jButton1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField meses;
    private javax.swing.JTextField nmNivel;
    private javax.swing.JTextField numTurmas;
    private javax.swing.JTextField v_valor;
    // End of variables declaration//GEN-END:variables

    public void setNivel(String nm_nivel) {
        this.nm_nivel = nm_nivel;
    }

    public String getNmNivel() {
        return this.nm_nivel;
    }

    public void initValue() throws SQLException {
        if (getNmNivel() != null) {
            NivellDao p = new NivellDao();
            CadastrarIdioma c = new CadastrarIdioma();
            ArrayList<String> ass = p.consultaTurmaNivel(nm_nivel);
            ArrayList<String> t = c.consultaIdioma();

            for (int i = 0; i < t.size(); i++) {
                if (ass.get(0).toString().equals(t.get(i).toString())) {
                    i_idioma.setSelectedIndex(i);
                }

            }

            codNivel.setText(ass.get(1));
            nmNivel.setText(ass.get(2));
            meses.setText(ass.get(3));
            numTurmas.setText(ass.get(4));

            jButton1.setEnabled(true);

        }
    }
}
